package com.mini.dubbo.consumer;

import java.util.Properties;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

import com.mini.dubbo.api.IGoodbyeService;
import com.mini.dubbo.api.IHelloService;
import com.mini.dubbo.consumer.discovery.IServiceDiscovery;
import com.mini.dubbo.consumer.discovery.RedisServiceDiscovery;

/**
 *
 * 结下，
 *
 * 1.BeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry可以动态将Bean注册。(先执行)
 * 2.BeanFactoryPostProcessor.postProcessBeanFactory可以修改各个注册的Bean，(后执行)
 *
 * @author yzy
 * Created by yzy on 2021-12-04
 */
public class CustomBeanDefinitionRegistry implements BeanDefinitionRegistryPostProcessor{

    /**
     * @RpcAnnotation 注解相当于 Dubbo 的 @Serivce
     * 可以自定义一个 注解，相当于 Dubbo 的 @Refrence，完成代理注入，就不用HELLO_SERVICE_SPRING_BEAN_NAME 和 GOOD_BYE_SPRING_BEAN_NAME了
     */
    public static final String HELLO_SERVICE_SPRING_BEAN_NAME = "helloService_spring";
    public static final String GOOD_BYE_SPRING_BEAN_NAME = "goodbyeService_spring";

    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        IServiceDiscovery serviceDiscovery = new RedisServiceDiscovery();
        RpcClientProxy rpcClientProxy = new RpcClientProxy(serviceDiscovery);

        IHelloService helloServiceProxy  = rpcClientProxy.create(IHelloService.class);
        beanFactory.registerSingleton(HELLO_SERVICE_SPRING_BEAN_NAME, helloServiceProxy);

        IGoodbyeService goodbyeServiceProxy = rpcClientProxy.create(IGoodbyeService.class);
        beanFactory.registerSingleton(GOOD_BYE_SPRING_BEAN_NAME, goodbyeServiceProxy);

    }

    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
    }
}
